//
//  snow.m
//  加载动画
//
//  Created by rimi on 16/3/17.
//  Copyright © 2016年 com.ruifeng. All rights reserved.
//

#define CPM(theX, theY) CGPointMake(theX, theY)
#import "Snow.h"

@implementation Snow
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setSnowView];
    }
    return self;
}
- (NSInteger)snowDegree{
    if (!_snowDegree) {
        _snowDegree = 120;
    }
    return _snowDegree;

}

- (instancetype)initWithFrame:(CGRect)frame snowDegree:(NSInteger)degree{
    
    self = [super initWithFrame:frame];
    if (self) {
        _snowDegree = self.snowDegree;
        [self setSnowView];
       
    }
    return self;
}

- (void)setSnowView{
    self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(handleAction:)];
    //调整雪的数量，数值越小雪越大
    self.displayLink.frameInterval = self.snowDegree;
    [self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
    //加载云
    double width = self.bounds.size.width;
    double height =self.bounds.size.height;
    self.cloud = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, width*0.84, height*0.54)];
    self.backgroundColor = [UIColor whiteColor];
    self.cloud.image = [UIImage imageNamed:@"云"];
    [self addSubview:self.cloud];
    //云左右飘动动画
    CAKeyframeAnimation * keyframe = [CAKeyframeAnimation animationWithKeyPath:@"position.x"];
    keyframe.repeatCount = HUGE_VAL;
    keyframe.values = @[@(self.bounds.size.width*0.3),@(self.bounds.size.width*0.55),@(self.bounds.size.width*0.3)];
    keyframe.duration = 18;
    [_cloud.layer addAnimation:keyframe forKey:nil];
    _mask = [[UIView alloc]initWithFrame:CGRectMake(0, self.bounds.size.height*0.95, self.bounds.size.width, self.bounds.size.height*0.3)];
    _mask.backgroundColor = [UIColor whiteColor];
    [self addSubview:_mask];
    

}

//加载雪的动画
- (void)handleAction:(CADisplayLink *)displayLink{
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"雪"]];
    CGFloat scale = arc4random_uniform(10)/100.0+0.3;
    imageView.transform = CGAffineTransformMakeScale(scale, scale);
    CGSize winSize = self.bounds.size;
    CGFloat x = arc4random_uniform(winSize.width*0.5)+winSize.width*0.2;
    CGFloat y =  self.bounds.size.height*0.45;  //-imageView.frame.size.height
    imageView.center = CGPointMake(x, y);
    
    [self addSubview:imageView];
    //将控件提到最上层
    [self bringSubviewToFront:self.cloud];
    [self bringSubviewToFront:_mask];
   
    [UIView animateWithDuration:arc4random_uniform(5)+5 animations:^{
        CGFloat toX = arc4random_uniform(winSize.width*0.85);
        CGFloat toY = imageView.frame.size.height * 0.5 + winSize.height;
        imageView.center = CGPointMake(toX, toY);
        imageView.transform = CGAffineTransformRotate(imageView.transform, arc4random_uniform(M_PI * 2));
        imageView.alpha = 0.6;
        imageView.transform = CGAffineTransformMakeScale(scale*1.1, scale*1.1);
    } completion:^(BOOL finished) {
        [imageView removeFromSuperview];
        NSLog(@"11");
    }];
}
//控件消失响应动画
- (void)disMiss{
    [UIView animateWithDuration:0.5 animations:^{
        self.transform = CGAffineTransformMakeScale(0.1, 0.1);
        self.alpha = 0;
    } completion:^(BOOL finished){
        [self removeFromSuperview];
    }];
}

     




@end
